<#setting number_format="#">
<#macro buildParameter p>
    <#if p.config.query.validation?? && p.config.query.validation.dateTime?? && p.config.query.validation.dateTime.enabled>
                .parameter("${p.name}", "")${'\t// '}<#if p.description?? && (p.description?length > 0)>${p.description}<br/></#if>格式：`${((p.config.query.validation.dateTime.pattern!"")?length > 0)?string(p.config.query.validation.dateTime.pattern!"", "yyyy-MM-dd")}<#if p.config.query.validation.dateTime.single><#else> / ${((p.config.query.validation.dateTime.pattern!"")?length > 0)?string(p.config.query.validation.dateTime.pattern!"", "yyyy-MM-dd")}`</#if>")
    <#else>
                .parameter("${p.name}", "${p.demoValue!""}")<#if (p.description??)>${'\t// '}${p.description}</#if>
    </#if>
</#macro>
/*
 * Copyright ${.now?string("yyyy")} the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ${app.packageName}.controller;

import ${app.packageName}.dto.*;
import ${app.packageName}.vo.${api.name?cap_first}VO;
import net.ymate.platform.commons.json.JsonWrapper;
import net.ymate.platform.commons.util.FileUtils;
import net.ymate.platform.core.annotation.EnableAutoScan;
import net.ymate.platform.core.annotation.EnableBeanProxy;
import net.ymate.platform.core.annotation.EnableDevMode;
import net.ymate.platform.core.beans.annotation.Inject;
import net.ymate.platform.mock.MockWebRequestHelper;
import net.ymate.platform.mock.web.MockHttpServletResponse;
import net.ymate.platform.mock.web.MockServletContext;
import net.ymate.platform.test.YMPJUnit4ClassRunner;
import net.ymate.platform.webmvc.WebMVC;
import net.ymate.platform.webmvc.base.Type;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.*;
import org.junit.runner.RunWith;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.OutputStream;
import java.nio.file.Files;

/**
 * <#if api.description?? && (api.description?length > 0)>${api.description}<br></#if>
 *
 * ${api.name?cap_first}ControllerTest generated By CrudMojo on ${.now?string("yyyy/MM/dd a HH:mm")}
 *
 * @author ${app.author!"YMP (https://www.ymate.net/)"}
 * @version ${app.version!"1.0.0"}
 */
@RunWith(YMPJUnit4ClassRunner.class)
@EnableAutoScan
@EnableBeanProxy
@EnableDevMode
public class ${api.name?cap_first}ControllerTest {

    @Inject
    private WebMVC webmvc;

    private final MockServletContext servletContext = new MockServletContext();

    @BeforeClass
    public static void setUpClass() {
    }

    @AfterClass
    public static void tearDownClass() {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }

    <#if !(api.settings??) || api.settings.enableQuery!true>@Test
    public void testQuery() throws Exception {
        MockHttpServletResponse response = MockWebRequestHelper.create(webmvc, servletContext)
                .get("${api.mapping}/query")
        <#if multiPrimaryKey><#list primaryFields as p><#if p.config?? && p.config.query?? && p.config.query.enabled>
                <@buildParameter p/>
        </#if></#list><#elseif primaryKey?? && primaryKey.config?? && primaryKey.config.query?? && primaryKey.config.query.enabled><@buildField primaryKey/></#if>
        <#list normalFields as p><#if p.config?? && p.config.query?? && p.config.query.enabled>
                <@buildParameter p/>
        </#if></#list>
                .parameter("page", "1")
                .parameter("pageSize", "20")
                .parameter(Type.Const.PARAM_FORMAT, Type.Const.FORMAT_JSON)
                .doFilter();
        Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatus());
        JsonWrapper jsonWrapper = JsonWrapper.fromJson(response.getContentAsString());
        System.out.println(response.getStatus() + " - " + jsonWrapper.getAsJsonObject().toString(true, true));
    }

    <#if !api.view>@Test
    public void testDetail() throws Exception {
        MockHttpServletResponse response = MockWebRequestHelper.create(webmvc, servletContext)
                .get("${api.mapping}/detail")
        <#list primaryFields as p>
                <@buildParameter p/>
        </#list>
                .parameter(Type.Const.PARAM_FORMAT, Type.Const.FORMAT_JSON)
                .doFilter();
        Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatus());
        JsonWrapper jsonWrapper = JsonWrapper.fromJson(response.getContentAsString());
        System.out.println(response.getStatus() + " - " + jsonWrapper.getAsJsonObject().toString(true, true));
    }</#if>

    <#if !(api.settings??) || api.settings.enableCreate!true>@Test
    public void testCreate() throws Exception {
        MockHttpServletResponse response = MockWebRequestHelper.create(webmvc, servletContext)
                .post("${api.mapping}/create")
        <#if multiPrimaryKey><#list primaryFields as p>
                <@buildParameter p/>
        </#list>
        <#elseif primaryKey?? && primaryKey.config.createOrUpdate.enabled>
                <@buildParameter p/>
        </#if>
        <#list normalFields as p><#if p.config?? && p.config.createOrUpdate?? && p.config.createOrUpdate.enabled>
                <@buildParameter p/>
        </#if></#list>
                .parameter(Type.Const.PARAM_FORMAT, Type.Const.FORMAT_JSON)
                .doFilter();
        Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatus());
        JsonWrapper jsonWrapper = JsonWrapper.fromJson(response.getContentAsString());
        System.out.println(response.getStatus() + " - " + jsonWrapper.getAsJsonObject().toString(true, true));
    }</#if>

    <#if !(api.settings??) || api.settings.enableUpdate!true>@Test
    public void testUpdate() throws Exception {
        MockHttpServletResponse response = MockWebRequestHelper.create(webmvc, servletContext)
                .post("${api.mapping}/update")
        <#list primaryFields as p>
                <@buildParameter p/>
        </#list>
        <#if multiPrimaryKey><#list primaryFields as p>
                <@buildParameter p/>
        </#list>
        <#elseif primaryKey?? && primaryKey.config.createOrUpdate.enabled>
                <@buildParameter p/>
        </#if>
        <#list normalFields as p><#if p.config?? && p.config.createOrUpdate?? && p.config.createOrUpdate.enabled>
                <@buildParameter p/>
        </#if></#list>
        <#if lastModifyTimeProp?? && !lastModifyTimeProp.foreign>
                <@buildParameter lastModifyTimeProp/>
        </#if>
                .parameter(Type.Const.PARAM_FORMAT, Type.Const.FORMAT_JSON)
                .doFilter();
        Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatus());
        JsonWrapper jsonWrapper = JsonWrapper.fromJson(response.getContentAsString());
        System.out.println(response.getStatus() + " - " + jsonWrapper.getAsJsonObject().toString(true, true));
    }<#list api.properties as p><#if !p.primary && p.config?? && p.config.status??><#list p.config.status as s><#if s.enabled>

    @Test
    public void test${s.methodName?cap_first}() throws Exception {
        MockHttpServletResponse response = MockWebRequestHelper.create(webmvc, servletContext)
                .post("${api.mapping}${s.mapping}")
        <#if multiPrimaryKey>
            <#list primaryFields as p>
                <@buildParameter p/>
            </#list>
        <#else>
                <@buildParameter primaryKey/>
        </#if>
        <#if s.reason>
                .parameter("reason", "")${'\t// '}${languageMap.reason}
        </#if>
                .parameter(Type.Const.PARAM_FORMAT, Type.Const.FORMAT_JSON)
                .doFilter();
        Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatus());
        JsonWrapper jsonWrapper = JsonWrapper.fromJson(response.getContentAsString());
        System.out.println(response.getStatus() + " - " + jsonWrapper.getAsJsonObject().toString(true, true));
    }</#if></#list></#if></#list></#if>

    <#if !(api.settings??) || api.settings.enableRemove!true>@Test
    public void testRemove() throws Exception {
        MockHttpServletResponse response = MockWebRequestHelper.create(webmvc, servletContext)
                .post("${api.mapping}/remove")
        <#if multiPrimaryKey>
            <#list primaryFields as p>
                    <@buildParameter p/>
            </#list>
        <#else>
                <@buildParameter primaryKey/>
        </#if>
                .parameter(Type.Const.PARAM_FORMAT, Type.Const.FORMAT_JSON)
                .doFilter();
        Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatus());
        JsonWrapper jsonWrapper = JsonWrapper.fromJson(response.getContentAsString());
        System.out.println(response.getStatus() + " - " + jsonWrapper.getAsJsonObject().toString(true, true));
    }</#if></#if>

    <#if !(api.settings??) || api.settings.enableExport!true>@Test
    public void testExport() throws Exception {
        MockHttpServletResponse response = MockWebRequestHelper.create(webmvc, servletContext)
                .get("${api.mapping}/export")
        <#if multiPrimaryKey><#list primaryFields as p><#if p.config?? && p.config.query?? && p.config.query.enabled>
                <@buildParameter p/>
        </#if></#list><#elseif primaryKey?? && primaryKey.config?? && primaryKey.config.query?? && primaryKey.config.query.enabled><@buildField primaryKey/></#if>
        <#list normalFields as p><#if p.config?? && p.config.query?? && p.config.query.enabled>
                <@buildParameter p/>
        </#if></#list>
                .doFilter();
        Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatus());
        String suffix = null;
        if (response.containsHeader(Type.HttpHead.CONTENT_DISPOSITION)) {
            String fileName = StringUtils.replace(StringUtils.substringAfter(response.getHeader(Type.HttpHead.CONTENT_DISPOSITION), "filename="), "\"", StringUtils.EMPTY);
            suffix = FileUtils.getExtName(fileName);
        }
        File tmpFile = File.createTempFile("export_", suffix);
        try (OutputStream outputStream = Files.newOutputStream(tmpFile.toPath())) {
            IOUtils.copyLarge(new ByteArrayInputStream(response.getContentAsByteArray()), outputStream);
        }
        System.out.println(tmpFile.getPath());
    }</#if>
}